# Tux Paint Sound Library - Sound library for tuxpaint plugins.

# Copyright (c) 2009 by Francisco Tufró
# nictuku@gmail.com
# http://www.tuxpaint.org/

# June 20, 2009


# The version number, for release:

VER_VERSION:=0.1
VER_DATE:=$(shell date +"%Y-%m-%d")

# Need to know the OS

SYSNAME:=$(shell uname -s)
ifeq ($(findstring MINGW32, $(SYSNAME)),MINGW32)
OS:=windows
else
ifeq ($(SYSNAME),Darwin)
OS:=osx
else
ifeq ($(SYSNAME),BeOS)
OS:=beos
else
ifeq ($(SYSNAME),Haiku)
OS:=beos
else
OS:=linux
endif
endif
endif
endif

# test if a library can be linked
linktest = $(shell if $(CC) $(CPPFLAGS) $(CFLAGS) -o dummy.o dummy.c $(LDFLAGS) $(1) $(2) > /dev/null 2>&1; \
	then \
		echo "$(1)"; \
	fi ;)

# test compiler options
comptest = $(shell if $(CC) $(CPPFLAGS) $(CFLAGS) $(1) $(2) -o dummy.o dummy.c $(LDFLAGS) > /dev/null 2>&1; \
	then \
		echo "$(1)"; \
	fi ;)

beos_RSRC_CMD:=xres -o tuxpaint src/tuxpaint.rsrc
RSRC_CMD:=$($(OS)_RSRC_CMD)

beos_MIMESET_CMD:=mimeset -f tuxpaint
MIMESET_CMD:=$($(OS)_MIMESET_CMD)

windows_SO_TYPE:=dll
osx_SO_TYPE:=bundle
beos_SO_TYPE:=so
linux_SO_TYPE:=so
SO_TYPE:=$($(OS)_SO_TYPE)

windows_LIBMINGW:=-lmingw32
LIBMINGW:=$($(OS)_LIBMINGW)

windows_EXE_EXT:=.exe
EXE_EXT:=$($(OS)_EXE_EXT)

windows_ARCH_LIBS:=obj/win32_print.o obj/resource.o
osx_ARCH_LIBS:=obj/postscript_print.o
beos_ARCH_LIBS:=obj/BeOS_print.o
linux_ARCH_LIBS:=obj/postscript_print.o
ARCH_LIBS:=$($(OS)_ARCH_LIBS)

PORTAUDIO:=$(call linktest,-lportaudio,)
SNDFILE:=$(call linktest,-lsndfile,)

windows_ARCH_LINKS:=-lintl -lwinspool -lshlwapi $(PORTAUDIO) $(SNDFILE) 
osx_ARCH_LINKS:=$(PORTAUDIO) $(SNDFILE)
beos_ARCH_LINKS:="-lintl $(PNG) -lz -lbe -liconv $(PORTAUDIO) $(SNDFILE)"
linux_ARCH_LINKS:=$(PORTAUDIO) $(SNDFILE)
ARCH_LINKS:=$($(OS)_ARCH_LINKS)

windows_ARCH_HEADERS:=src/win32_print.h
osx_ARCH_HEADERS:=
beos_ARCH_HEADERS:=src/BeOS_print.h
linux_ARCH_HEADERS:=
ARCH_HEADERS:=$($(OS)_ARCH_HEADERS)

# Where things will go when ultimately installed:
windows_PREFIX:=/usr/local
osx_PREFIX:=/usr/local
beos_PREFIX:=/boot/apps/Games/TuxPaint
linux_PREFIX:=/usr/local
PREFIX:=$($(OS)_PREFIX)


# Root directory to place files when creating packages.
# PKG_ROOT is the old name for this, and should be undefined.
# "TuxPaint-1" is the OLPC XO name. Installing to ./ is bad!
ifeq ($(PREFIX),./)
DESTDIR:=TuxPaint-1
else
DESTDIR:=$(PKG_ROOT)
endif

# Program:
LIB_PREFIX:=$(DESTDIR)$(PREFIX)/lib

# Data:
DATA_PREFIX:=$(DESTDIR)$(PREFIX)/share/tp_sound

# Locale files
LOCALE_PREFIX=$(DESTDIR)$(PREFIX)/share/locale

# IM files
IM_PREFIX=$(DESTDIR)$(PREFIX)/share/tp_sound/im

# Docs and man page:
DOC_PREFIX:=$(DESTDIR)$(PREFIX)/share/doc/tp_sound
DEVDOC_PREFIX:=$(DESTDIR)$(PREFIX)/share/doc/tp_sound-dev
MAN_PREFIX:=$(DESTDIR)$(PREFIX)/share/man
DEVMAN_PREFIX:=$(DESTDIR)$(PREFIX)/share/man

# Maemo flag
MAEMOFLAG:=

ifeq ($(hack),1)
hack:
	@echo 'LDFLAGS       is' $(LDFLAGS)
	@echo 'CFLAGS        is' $(CFLAGS)
	@echo 'CPPFLAGS      is' $(CPPFLAGS)
endif

# The entire set of CFLAGS:

#-ffast-math
OPTFLAGS:=-O2
CFLAGS:=$(CPPFLAGS) $(OPTFLAGS) -W -Wall -fno-common -ffloat-store \
	$(call comptest,-fvisibility=hidden,) \
	-Wcast-align -Wredundant-decls \
	-Wbad-function-cast -Wwrite-strings \
	-Waggregate-return \
	-Wstrict-prototypes -Wmissing-prototypes \
	$(shell src/test-option.sh -Wstrict-aliasing=2)

DEFS:=-DVER_DATE=\"$(VER_DATE)\" -DVER_VERSION=\"$(VER_VERSION)\" \
	-DDATA_PREFIX=\"$(patsubst $(DESTDIR)%,%,$(DATA_PREFIX))/\" \
	-DDOC_PREFIX=\"$(patsubst $(DESTDIR)%,%,$(DOC_PREFIX))/\" \
	-DLOCALEDIR=\"$(patsubst $(DESTDIR)%,%,$(LOCALE_PREFIX))/\" \
	-DIMDIR=\"$(patsubst $(DESTDIR)%,%,$(IM_PREFIX))/\" \
	-DCONFDIR=\"$(patsubst $(DESTDIR)%,%,$(CONFDIR))/\"
DEBUG_FLAGS:=
#DEBUG_FLAGS:=-g

.SUFFIXES:

#############################################################################
#############################################################################
#############################################################################
#
# "make" with no arguments builds the program and man page from sources:
#
.PHONY: all
all:	tp_sound
	@echo
	@echo "--------------------------------------------------------------"
	@echo
	@echo "Done compiling."
	@echo
	@echo "Now run 'make install' with any options you ran 'make' with."
	@echo "to install tp_sound."
	@echo
	@echo "You may need superuser ('root') privileges, depending on"
	@echo "where you're installing."
	@echo "(Depending on your system, you either need to 'su' first,"
	@echo "or run 'sudo make install'.)"
	@echo

######

windows_ARCH_INSTALL:=
osx_ARCH_INSTALL:=
beos_ARCH_INSTALL:=
linux_ARCH_INSTALL:=
ARCH_INSTALL:=$($(OS)_ARCH_INSTALL)

# "make install" installs all of the various parts
# (depending on the *PREFIX variables at the top, you probably need
# to do this as superuser ("root"))
.PHONY: install
install: install-tp_sound $(ARCH_INSTALL)
	@echo "--------------------------------------------------------------"
	@echo "All done!"
	@echo

.PHONY: install-tp_sound
install-tp_sound:
	@echo "...Installing Tux Paint Sound Library..."
	@install -d $(PREFIX)
	@install -d $(PREFIX)/lib
	@install -d $(PREFIX)/include
	@cp *.$(SO_TYPE) $(PREFIX)/lib/
	@cp src/tp_sound.h $(PREFIX)/include/
	@chmod a+r,g-w,o-w $(PREFIX)/lib/*.$(SO_TYPE)

# "make clean" deletes the program, the compiled objects and the
# built man page (returns to factory archive, pretty much...)
.PHONY: clean
clean:
	@echo
	@echo "Cleaning up the build directory! ($(PWD))"
	@-rm -f *.$(SO_TYPE)
	@-rm -f obj/*.o
	@-rm -f dummy.o
	@#if [ -d obj ]; then rmdir obj; fi
	@echo


# Make the "obj" directory to throw the object(s) into:
# (not necessary any more; bjk 2006.02.20)

obj:
	@mkdir obj

######

TP_SOUND_ARCH_LINKS:= $(PORTAUDIO) $(SNDFILE)

windows_PLUGIN_LIBS:= $(TP_SOUND_ARCH_LINKS)
osx_PLUGIN_LIBS:= $(TP_SOUND_ARCH_LINKS)
beos_PLUGIN_LIBS:="$(TP_SOUND_ARCH_LINKS)"
linux_PLUGIN_LIBS:=$(TP_SOUND_ARCH_LINKS)
PLUGIN_LIBS:=$($(OS)_PLUGIN_LIBS)

TP_SOUND_CFLAGS:=-g3 -O2 -fno-common -W -Wstrict-prototypes -Wmissing-prototypes -Wall -Isrc/
SHARED_FLAGS:=-shared -fpic

TP_SOUND_C:=$(wildcard src/*.c)
TP_SOUND_SO:=$(patsubst src/%.c,./%.$(SO_TYPE),$(TP_SOUND_C))

$(TP_SOUND_SO): ./%.$(SO_TYPE): ./src/%.c  
	$(CC) $(TP_SOUND_CFLAGS) $(SHARED_FLAGS) $(DEFS) -o lib$@ $< $(PLUGIN_LIBS)

.PHONY: tp_sound
tp_sound: $(TP_SOUND_SO)
